package com.abc.easyclaim;


import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import com.abc.bean.Case;
import com.abc.db.ConfigData;
import com.abc.db.DBManager;

import android.app.Application;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

public class MainApplication extends Application {
	private static String sdPath;
	private static DBManager mgr;
	private static Context mContext;
	private static ConfigData configData ;
	private static Map currentDataMap = new HashMap(); 
	private static Map<String, Bitmap> carLogoMap = new HashMap<String, Bitmap>();
	private static int currentPlay=-1;           //后台正在播放文件序号 
	@Override
	public void onCreate() {
		super.onCreate();
		
		mContext = getApplicationContext();
		configData = new ConfigData(mContext);
		mgr = new DBManager(mContext);
		sdPath = android.os.Environment.getExternalStorageDirectory()
				 .getAbsolutePath();
		Log.d("sys", "配置信息已加载");
		Bitmap bm=null;
		try {
			bm = BitmapFactory.decodeStream(mContext.getResources().getAssets().open("car/"+"OTHER"+".png"));
			carLogoMap.put("OTHER", bm);
			Log.d("sys", " found picture:"+"OTHER"+".png");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	}
	
	/**获取Context.
	* @return
	*/
	public static Context getContext(){
		return mContext;
	}
	
	public static ConfigData getConfigData(){
		return configData;
	}
	
	public static void saveConfigData(){
		configData.saveData();
		Log.d("sys", "配置信息已保存");
	}
	
	public static Bitmap getCarLogoImage(String logoName){
		if(logoName==null)
			logoName="OTHER";
		if(!carLogoMap.containsKey(logoName)){
			Bitmap bm=null;
			try {
				bm = BitmapFactory.decodeStream(mContext.getResources().getAssets().open("car/"+logoName+".png"));
				carLogoMap.put(logoName, bm);
				Log.d("sys", " found picture:"+logoName+".png");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
			if(bm==null){
				Log.d("sys", "not found picture:"+logoName+".png");
				return carLogoMap.get("OTHER");
			}else {
				return carLogoMap.get(logoName);
			}
		}else {
			return carLogoMap.get(logoName);
		}
	
	}



	public static DBManager getMgr() {
		return mgr;
	}

	public static void setMgr(DBManager mgr) {
		MainApplication.mgr = mgr;
	}

	public static Integer getCurrentCaseId() {
		return (Integer) currentDataMap.get("caseId");
	}

	public static void setCurrentCaseId(Integer currentCaseId) {
		currentDataMap.put("caseId", currentCaseId);
	}

	public static String getSdPath() {
		return sdPath;
	}
	
	public int getState(){  
	    return currentPlay;  
    }  
	
	public void setState(int s){  
	    currentPlay = s;  
	}  
	
}
